# -*- coding: utf-8 -*-

import falcon
from falcon_auth import FalconAuthMiddleware
import shortuuid
# from falcon_swagger_ui import register_swaggerui_app
# from falconswagger import SwaggerAPI, ModelHttpMeta

from app import log
from app.middleware import Benchmark, AutoCRUDMiddleware
from app.database import db_session, init_session , engine
from app.api.common import base, IndexResource
from app.config import APP_ENV

from app.errors import AppError
from app.utils import *
# from app.api.v1 import ProductResource

LOG = log.get_logger()
# add route and Resource maps
resources = [
    # {endpoint('product'): ProductResource(db_engine=engine)},
]


class App(falcon.API):
    def __init__(self, *args, **kwargs):
        super(App, self).__init__(*args, **kwargs)
        LOG.info('API Server is starting')
        routes = []
        for res in resources:
            for route, resource in res.items():
                print (route)
                routes.append(route)
                # LOG.info(route)
                self.add_route(route, resource)


        self.add_route('/', IndexResource(['/' + name for name in routes]))

        self.add_error_handler(AppError, AppError.handle)

# init app 
def init_app():
    init_session()
    middleware = [Benchmark(), AutoCRUDMiddleware()]
    application = App(middleware=middleware)
    return application
